package com.tao.service.impl;

import com.tao.mapper.MenuMapper;
import com.tao.pojo.entity.Menu;
import com.tao.pojo.vo.MenuVo;
import com.tao.service.CommonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class CommonService1 implements CommonService {
    @Autowired
    MenuMapper menuMapper;
    @Override
    public List<MenuVo> getMenu(Integer roleId) {
        List<Menu> menus = menuMapper.selectMenu(roleId);
        List<MenuVo> menuVos = new ArrayList<>();
        menus.forEach(menu -> {
            if (menu.getParentId()==null) {
                MenuVo menuVo = new MenuVo();
                BeanUtils.copyProperties(menu, menuVo);
                menuVos.add(menuVo);
            }
        });
        menus.forEach(menu -> {
           if(menu.getParentId()!=null) {
                menuVos.forEach(menuVo -> {
                    if (menuVo.getId().equals(menu.getParentId())){
                        menuVo.getChildren().add(menu);
                    }
                });
            }
        });
        return menuVos;
    }
}
